﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PaliTextReader2.Utilities;
using Microsoft.Win32;
using System.IO;

namespace PaliTextReader2
{
    static class Program
    {
        public static MainWnd TopWindow;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Utility.LibraryPath = GetLibraryFolder();
            if (Utility.LibraryPath == "")
            {
                MessageBox.Show("You must install pali canon library in order to run this application");
            }
            else
            {
                if (FolderPermission.IsFolderWriteable(Utility.LibraryPath))
                {
                    AppDomain.CurrentDomain.SetData("DataDirectory", Utility.LibraryPath);
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    TopWindow = new MainWnd();
                    Application.Run(TopWindow);
                }
                else
                {
                    MessageBox.Show("You don't have write permissions to the library folder\r\n" + Utility.LibraryPath+"\r\nThe application will now exit.");
                }
            }
        }

        private static string GetLibraryFolder()
        {
            string strRetVal=String.Empty;
            object objValue=Registry.GetValue(Registry.LocalMachine + "\\Software\\PaliTextReader2", "LibraryDir", "");
            if (objValue == null)
            { 
                objValue = Registry.GetValue(Registry.LocalMachine + "\\Software\\Wow6432Node\\PaliTextReader2", "LibraryDir", "");
                if (objValue != null)
                    strRetVal = objValue.ToString();
            }
            else
                strRetVal=objValue.ToString();

            return strRetVal;
        }
    }
}
